#include "PWM_control.h"
char is_use_high_pwm = 1;
uint32_t disable_pwm_counters;
void Pull_Down_all_PWM(void)
{
    SD_ALL_OFF;
    SWITCH_ALL_PIN_TO_IO_OUTPUT(); // change WL pin to IO output
    SD_ALL_ON;
    // printf("stoptimmes:%d,Pull Down\r\n", disable_pwm_counters);
    //  printf("tim1_period:%d tim_ccr1:%d tim_ccr2:%d tim_ccr3:%d tim_ccr4:%d\r\n", PWM_PERIOD, (uint32_t)TIM1->CCR1, (uint32_t)TIM1->CCR2, (uint32_t)TIM1->CCR3, (uint32_t)TIM1->CCR4);
    OUT_PUT_TURN_TO_HiGH_MOSFET();
    OUT_PUT_TURN_TO_LOW_MOSFET();

    // SD_ALL_ON;
}

void OUT_PUT_TURN_TO_LOW_MOSFET(void) // Setup PB15 pin turn on MOSFET when it is in IO mode
{

#ifdef WL_PIN_IO_MODE_LOW_ACTIVE
    WL_PIN_IO_MODE_OUTPUT_LOW; // GPIOB->BRR |= GPIO_Pin_15;//setup IO pin output low //20190626
    UL_PIN_IO_MODE_OUTPUT_LOW;
    VL_PIN_IO_MODE_OUTPUT_LOW;
    WH_PIN_IO_MODE_OUTPUT_HIGH; // GPIOB->BSRR |= GPIO_Pin_15;//setup IO pin output high//20190626
    VH_PIN_IO_MODE_OUTPUT_HIGH;
    UH_PIN_IO_MODE_OUTPUT_HIGH;
#else
    WL_PIN_IO_MODE_OUTPUT_HIGH; // GPIOB->BSRR |= GPIO_Pin_15;//setup IO pin output high//20190626
    VL_PIN_IO_MODE_OUTPUT_HIGH;
    UL_PIN_IO_MODE_OUTPUT_HIGH;
    WH_PIN_IO_MODE_OUTPUT_LOW; // GPIOB->BRR |= GPIO_Pin_15;//setup IO pin output low //20190626
    UH_PIN_IO_MODE_OUTPUT_LOW;
    VH_PIN_IO_MODE_OUTPUT_LOW;

#endif
}
void OUT_PUT_TURN_TO_HiGH_MOSFET(void) // Setup PB15 pin turn on MOSFET when it is in IO mode
{

#ifdef WL_PIN_IO_MODE_LOW_ACTIVE
    WL_PIN_IO_MODE_OUTPUT_HIGH; // GPIOB->BRR |= GPIO_Pin_15;//setup IO pin output low //20190626
    UL_PIN_IO_MODE_OUTPUT_HIGH;
    VL_PIN_IO_MODE_OUTPUT_HIGH;
    WH_PIN_IO_MODE_OUTPUT_LOW; // GPIOB->BSRR |= GPIO_Pin_15;//setup IO pin output high//20190626
    VH_PIN_IO_MODE_OUTPUT_LOW;
    UH_PIN_IO_MODE_OUTPUT_LOW;
#else
    WL_PIN_IO_MODE_OUTPUT_LOW; // GPIOB->BSRR |= GPIO_Pin_15;//setup IO pin output high//20190626
    VL_PIN_IO_MODE_OUTPUT_LOW;
    UL_PIN_IO_MODE_OUTPUT_LOW;
    WH_PIN_IO_MODE_OUTPUT_HIGH; // GPIOB->BRR |= GPIO_Pin_15;//setup IO pin output low //20190626
    UH_PIN_IO_MODE_OUTPUT_HIGH;
    VH_PIN_IO_MODE_OUTPUT_HIGH;

#endif
}
void SWITCH_ALL_PIN_TO_IO_OUTPUT(void) // PB15 pin change to IO output mode
{
    GPIO_InitTypeDef GPIO_InitStructure;

    OUT_PUT_TURN_TO_LOW_MOSFET(); // turn off MOSFET when this WL pin in IO output mode

    GPIO_InitStructure.GPIO_Pin = WL_PIN_NUM; // IO Output
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    ; //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(WL_PIN_GROUP, &GPIO_InitStructure);
    // WL pin setup to IO output mode
    GPIO_InitStructure.GPIO_Pin = UL_PIN_NUM | VL_PIN_NUM | UH_PIN_NUM | VH_PIN_NUM | WH_PIN_NUM; // IO Output
    GPIO_Init(UL_PIN_GROUP, &GPIO_InitStructure);                                                 // WL pin setup to IO output mode
}
void SWITCH_LOW_PIN_TO_SD_OUTPUT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    OUT_PUT_TURN_TO_LOW_MOSFET(); // turn off MOSFET when this WL pin in IO output mode

    GPIO_InitStructure.GPIO_Pin = WL_PIN_NUM; // IO Output
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(WL_PIN_GROUP, &GPIO_InitStructure);
    // WL pin setup to IO output mode
    GPIO_InitStructure.GPIO_Pin = UL_PIN_NUM | VL_PIN_NUM; // IO Output
    GPIO_Init(UL_PIN_GROUP, &GPIO_InitStructure);          // WL pin setup to IO output mode
}
void SWITCH_LOW_PIN_TO_SD_float(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    OUT_PUT_TURN_TO_LOW_MOSFET(); // turn off MOSFET when this WL pin in IO output mode

    GPIO_InitStructure.GPIO_Pin = WL_PIN_NUM; // IO Output
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(WL_PIN_GROUP, &GPIO_InitStructure);
    // WL pin setup to IO output mode
    GPIO_InitStructure.GPIO_Pin = UL_PIN_NUM | VL_PIN_NUM; // IO Output
    GPIO_Init(UL_PIN_GROUP, &GPIO_InitStructure);          // WL pin setup to IO output mode
}
void LOW_PIN_SD_TURN_ON(void)
{
    WL_PIN_IO_MODE_OUTPUT_HIGH; // GPIOB->BRR |= GPIO_Pin_15;//setup IO pin output low //20190626
    UL_PIN_IO_MODE_OUTPUT_HIGH;
    VL_PIN_IO_MODE_OUTPUT_HIGH;
}
void LOW_PIN_SD_TURN_OFF(void)
{
    WL_PIN_IO_MODE_OUTPUT_LOW; // GPIOB->BSRR |= GPIO_Pin_15;//setup IO pin output high//20190626
    VL_PIN_IO_MODE_OUTPUT_LOW;
    UL_PIN_IO_MODE_OUTPUT_LOW;
}
void redefine_all_PWM_Pin(void) //
{

    GPIO_InitTypeDef GPIO_InitStructure;
    //    printf("pwm_out\r\n");
    SD_ALL_OFF;
    OUT_PUT_TURN_TO_LOW_MOSFET(); // turn off MOSFET when this UL pin in IO output mode

    GPIO_InitStructure.GPIO_Pin = WL_PIN_NUM;       // TIM1_CH1N输出
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(WL_PIN_GROUP, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = UL_PIN_NUM | VL_PIN_NUM | UH_PIN_NUM | VH_PIN_NUM | WH_PIN_NUM;
    GPIO_Init(UL_PIN_GROUP, &GPIO_InitStructure);

    GPIO_PinAFConfig(UH_PIN_GROUP, UH_PIN_SOURCE_NUM, UH_PIN_AF_NUM);
    GPIO_PinAFConfig(VH_PIN_GROUP, VH_PIN_SOURCE_NUM, VH_PIN_AF_NUM);
    GPIO_PinAFConfig(WH_PIN_GROUP, WH_PIN_SOURCE_NUM, WH_PIN_AF_NUM);
    GPIO_PinAFConfig(UL_PIN_GROUP, UL_PIN_SOURCE_NUM, UL_PIN_AF_NUM);
    GPIO_PinAFConfig(VL_PIN_GROUP, VL_PIN_SOURCE_NUM, VL_PIN_AF_NUM);
    GPIO_PinAFConfig(WL_PIN_GROUP, WL_PIN_SOURCE_NUM, WL_PIN_AF_NUM); // UL Pin setup pin to TIM1_CH1N PWM output mode
    SD_ALL_ON;
}
void take_driver_all_off(void)
{
    GPIOD->BRR |= GPIO_Pin_6;
    printf("stop_pwm");
}
void take_driver_all_on(void)
{
    GPIOD->BSRR |= GPIO_Pin_6; // SD_port_ON
    printf("start_pwm");
}
